package com.fyzk.activity.user;

import android.content.Context;
import android.util.AttributeSet;
import android.util.TypedValue;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.fyzk.utils.Utils;
import com.tyjc.lottery.R;

public class UserItemView extends LinearLayout{
	
	private Context context;
	private LinearLayout ll;
	private String[] names;
	private int[] icons;
	private int[] bgs;
	
	private OnUserItemListener listener;
	
	public UserItemView(Context context, AttributeSet attrs,
			int defStyle) {
		super(context, attrs, defStyle);
		this.context = context;
		initView();
	}
	
	public UserItemView(Context context, AttributeSet attrs) {
		super(context, attrs);
		this.context = context;
		initView();
	}
	
	public UserItemView(Context context) {
		super(context);
		this.context = context;
		initView();
	}
	

	
	private void initView(){
		View.inflate(this.context, R.layout.dj_usercenteritem, this);
		ll = (LinearLayout) findViewById(R.id.user_itemview);
	}
	
	public void addViews(){
		for (int i = 0; i < names.length; i++) {
			ll.addView(getItemView(i));
			View view = new View(context);
			if(i != names.length - 1 ){
				LayoutParams params = new LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, 1);
				params.setMargins(Utils.dip2px(50), 0, 0, 0);
				view.setLayoutParams(params);
				view.setBackgroundColor(getResources().getColor(R.color.gray_line));
				ll.addView(view);
			}
		}
	}
	
	private View getItemView(final int i){
		View view = LayoutInflater.from(context).inflate(R.layout.dj_usercenter1_item, null);
		RelativeLayout rl = (RelativeLayout) view.findViewById(R.id.user_item_rl);
		ImageView icon_iv = (ImageView) view.findViewById(R.id.user_item_iv);
		TextView name_tv = (TextView) view.findViewById(R.id.user_item_tv);
		ImageView jianTou_tv = (ImageView) view.findViewById(R.id.user_editnickname);

		if (icons != null && icons.length > i && icons[i] != 0) {
			icon_iv.setVisibility(View.VISIBLE);
			icon_iv.setImageResource(icons[i]);
		}

		if (names != null && names.length > i) {
			name_tv.setText(names[i]);
		}
		
		if (bgs != null && bgs.length > i) {
			rl.setBackgroundColor(getResources().getColor(R.color.white));
		}
		
//		if (i == 0) {
//			name_tv.setGravity(Gravity.BOTTOM);
//			name_tv.setTypeface(Typeface.defaultFromStyle(Typeface.BOLD));
//			name_tv.setTextSize(TypedValue.COMPLEX_UNIT_SP, 20);
//			name_tv.setTextColor(getResources().getColor(R.color.color_333333));
//			jianTou_tv.setVisibility(View.INVISIBLE);
//		}else{
			name_tv.setTextSize(TypedValue.COMPLEX_UNIT_SP, 14);
			name_tv.setTextColor(getResources().getColor(R.color.color_666666));
//		}
		
//		if(i == names.length-1 || i == 0){
//			line.setVisibility(View.INVISIBLE);
//		}
//
		rl.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				if (listener != null) {
					listener.onUserItem(i);
				}
				
			}
		});
		
		return view;
	}
	
	public interface OnUserItemListener{
		void onUserItem(int itemId);
	}
	
	public void setOnUserItemListener(OnUserItemListener listener){
		this.listener = listener;
	}

	public void setNames(int id) {
		this.names = context.getResources().getStringArray(id);
	}
	public void setNames(String[] names) {
		this.names = names;
	}

	public void setIcons(int id) {
		this.icons = context.getResources().getIntArray(id);
	}
	public void setIcons(int[] icons) {
		this.icons = icons;
	}

	public void setBgs(int id) {
		this.bgs = context.getResources().getIntArray(id);
	}
	public void setBgs(int[] bgs) {
		this.bgs = bgs;
	}
	
	
	

}
